home *** CD-ROM | disk | FTP | other *** search
- /*
- File: AVTransportFamily.c
-
- Contains: Services provided by the AV transport control driver family.
-
- Version: 1.0
-
- Written by: Erik Staats
-
- Copyright: © 1996-1998 by Apple Computer, Inc., all rights reserved.
-
- File Ownership:
-
- DRI: Eric Anderson
-
- Other Contact:
-
- Technology: FireWire
-
- Writers:
-
- (jkl) Jay Lloyd
-
- Change History (most recent first):
-
- <8> 1/15/98 jkl Update for new headers.
- <FW7> 3/18/97 ES Changed to not create new AVT driver if we get device added
- notification for a device we already have in our device list.
- <FW6> 2/7/97 ES Removed calls to AESendSystemEvent. Added equivalent
- implementation. Added GetNextAVTClientEvent.
- <FW5> 10/22/96 ES Changed to use generic driver model. Changed to use Generic
- Driver Family notification rather than DevNLib notification.
- <FW4> 8/1/96 ES Took out unused local variables.
- <FW3> 6/27/96 ES Took out include of AdminMessagePort.h.
- <FW2> 6/20/96 ES Filled in contains and written by fields.
- <FW1> 6/20/96 ES first checked in
-
- */
-
- #include <Types.h>
- #include <Errors.h>
- #include <Strings.h>
- #include <Traps.h>
- #include <Events.h>
- #include <Processes.h>
- #include <AppleEvents.h>
- #include <Devices.h>
- #include <CodeFragments.h>
- #include <DriverServices.h>
- #include <NameRegistry.h>
- #include <GenericDriverFamily.h>
- #include <AVTransport.h>
- #include <AVTransportPriv.h>
- #include <AVTransportExpert.h>
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Define global structure to hold all of the AV transport control driver
- // Family data.
- //
-
- AVTFamilyDataPtr gpAVTFamilyData = nil;
- AVTExpertDataPtr gpAVTExpertData = nil;
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Internal routine prototypes.
- //
-
- static OSStatus AVTEventHandler (
- GDFDeviceEventDataPtr pGDFDeviceEventData);
-
- static OSStatus AVTHandleDeviceAdded (
- GDFDeviceEventDataPtr pGDFDeviceEventData);
-
- static OSStatus AVTHandleDeviceRemoved (
- GDFDeviceEventDataPtr pGDFDeviceEventData);
-
- static OSErr AVTSendClientAppleEvent (
- AVTClientDataPtr pAVTClientData,
- const AppleEvent *theAppleEvent,
- AESendMode sendMode,
- AESendPriority sendPriority,
- long timeOutInTicks,
- AEIdleUPP idleProc,
- AEFilterUPP filterProc);
-
- static void DisposeClientAppleEventData (
- ClientAppleEventDataPtr pClientAppleEventData);
-
- static OSStatus AVTCreateClientAppleEventQueue (
- AVTClientDataPtr pAVTClientData);
-
- static OSStatus AVTDisposeClientAppleEventQueue (
- AVTClientDataPtr pAVTClientData);
-
- static OSStatus AVTAddDriver (
- GDFDeviceEventDataPtr pGDFDeviceEventData,
- AVTDriverID *pAVTDriverID);
-
- static OSStatus CreateAVTDriverID (
- AVTDriverID *pAVTDriverID);
-
- static void DisposeAVTDriver (
- AVTDriverID avtDriverID);
-
- static AVTDriverID AVTFindDriver (
- RegEntryIDPtr pRegEntryID);
-
-
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Public routines.
- //
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // RegisterAVTClientApplication
- //
- // This routine registers the calling application as a client of the
- // AV transport control driver family.
- //
-
- OSStatus RegisterAVTClientApplication(
- AVTClientID *pAVTClientID,
- UInt32 clientContextData)
- {
- AVTClientID avtClientID = kInvalidAVTClientID;
- AVTClientDataPtr pAVTClientData = nil,
- pNextAVTClientData;
- ProcessSerialNumber currentPSN;
- OSStatus status = noErr;
-
- // Allocate client data record.
- pAVTClientData =
- (AVTClientDataPtr) PoolAllocateResident (sizeof (AVTClientData), true);
- if (pAVTClientData != nil)
- {
- pAVTClientData->clientContextData = clientContextData;
- avtClientID = (AVTClientID) pAVTClientData;
- }
- else
- {
- status = memFullErr;
- }
-
- // Add client data record to list.
- if (status == noErr)
- {
- pNextAVTClientData = gpAVTFamilyData->pAVTClientList;
- if (pNextAVTClientData != nil)
- pNextAVTClientData->pPrevAVTClientData = pAVTClientData;
-
- pAVTClientData->pPrevAVTClientData = nil;
- pAVTClientData->pNextAVTClientData = pNextAVTClientData;
- gpAVTFamilyData->pAVTClientList = pAVTClientData;
- }
-
- // Create client Apple Event queue.
- if (status == noErr)
- status = AVTCreateClientAppleEventQueue (pAVTClientData);
-
- // Create an Apple Event target descriptor for the client.
- if (status == noErr)
- {
- status = GetCurrentProcess (¤tPSN);
-
- if (status == noErr)
- {
- status = AECreateDesc (typeProcessSerialNumber,
- ¤tPSN,
- sizeof (ProcessSerialNumber),
- &(pAVTClientData->clientAEAddress));
- if (status == noErr)
- pAVTClientData->clientAEAddressValid = true;
- }
- }
-
- // Create an Apple Event for device added notification.
- if (status == noErr)
- {
- status = AECreateAppleEvent
- (kAEAVTransportEventClass,
- kAEAVTransportDeviceAdded,
- &(pAVTClientData->clientAEAddress),
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &(pAVTClientData->deviceAddedAppleEvent));
- if (status == noErr)
- pAVTClientData->deviceAddedAppleEventValid = true;
- }
-
- // Create an Apple Event for device removed notification.
- if (status == noErr)
- {
- status = AECreateAppleEvent
- (kAEAVTransportEventClass,
- kAEAVTransportDeviceRemoved,
- &(pAVTClientData->clientAEAddress),
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &(pAVTClientData->deviceRemovedAppleEvent));
- if (status == noErr)
- pAVTClientData->deviceRemovedAppleEventValid = true;
- }
-
- // Clean up on error.
- if (status != noErr)
- {
- if (avtClientID != kInvalidAVTClientID)
- UnregisterAVTClientApplication (avtClientID);
- }
-
- // Return results.
- *pAVTClientID = avtClientID;
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // UnregisterAVTClientApplication
- //
- // This routine unregisters the calling application as a client of the
- // AV transport control driver family.
- //
-
- OSStatus UnregisterAVTClientApplication(
- AVTClientID avtClientID)
- {
- AVTClientDataPtr pAVTClientData,
- pPrevAVTClientData,
- pNextAVTClientData;
- OSStatus status = noErr;
-
- if (avtClientID != kInvalidAVTClientID)
- {
- // Get client data from ID.
- pAVTClientData = (AVTClientDataPtr) avtClientID;
-
- // Deallocate device removed Apple Event.
- if (pAVTClientData->deviceRemovedAppleEventValid)
- AEDisposeDesc (&(pAVTClientData->deviceRemovedAppleEvent));
-
- // Deallocate device added Apple Event.
- if (pAVTClientData->deviceAddedAppleEventValid)
- AEDisposeDesc (&(pAVTClientData->deviceAddedAppleEvent));
-
- // Deallocate target descriptor.
- if (pAVTClientData->clientAEAddressValid)
- AEDisposeDesc (&(pAVTClientData->clientAEAddress));
-
- // Dispose of client Apple Event queue.
- if (pAVTClientData->clientAppleEventQueue != nil)
- AVTDisposeClientAppleEventQueue (pAVTClientData);
-
- // Remove client from list.
- pPrevAVTClientData = pAVTClientData->pPrevAVTClientData;
- pNextAVTClientData = pAVTClientData->pNextAVTClientData;
-
- if (pPrevAVTClientData != nil)
- pPrevAVTClientData->pNextAVTClientData = pNextAVTClientData;
- else
- gpAVTFamilyData->pAVTClientList = pNextAVTClientData;
-
- if (pNextAVTClientData != nil)
- pNextAVTClientData->pPrevAVTClientData = pPrevAVTClientData;
-
- // Deallocate client data record.
- PoolDeallocate ((Ptr) pAVTClientData);
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // GetAVTDriverList
- //
- // This routine returns a list of AV transport control driver IDs and the
- // number of drivers installed in the system.
- //
-
- OSStatus GetAVTDriverList(
- AVTDriverID *pAVTDriverIDList,
- UInt32 avtDriverIDListSize,
- UInt32 *pNumAVTDrivers)
- {
- AVTDriverDataPtr pAVTDriverData;
- UInt32 numAVTDrivers,
- numAVTDriversInList,
- driverNum;
- OSStatus status = noErr;
-
- // Compute number of drivers to put in list.
- numAVTDrivers = gpAVTFamilyData->numAVTDrivers;
- numAVTDriversInList = avtDriverIDListSize;
- if (numAVTDriversInList > numAVTDrivers)
- numAVTDriversInList = numAVTDrivers;
-
- // Copy driver IDs in order.
- pAVTDriverData = gpAVTFamilyData->pAVTDriverList;
- for (driverNum = 0; driverNum < numAVTDriversInList; driverNum++)
- {
- pAVTDriverIDList[driverNum] = (AVTDriverID) pAVTDriverData;
- pAVTDriverData = pAVTDriverData->pNextAVTDriverData;
- }
-
- // Return number of installed drivers.
- *pNumAVTDrivers = numAVTDrivers;
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // CallAVTDriver
- //
- // This routine sends a request to the given driver.
- //
-
- OSStatus CallAVTDriver(
- AVTDriverID avtDriverID,
- AVTInterfaceParamsPtr pAVTInterfaceParams)
- {
- AVTDriverDataPtr pAVTDriverData;
- CntrlParam cntrlParam;
- OSStatus status = noErr;
-
- // Get the driver data from the ID.
- pAVTDriverData = (AVTDriverDataPtr) avtDriverID;
-
- /*zzz*/
- if (pAVTDriverData->deviceDisconnected)
- return (-139);//zzz need disconnectedErr
- /*zzz*/
- // Set up control parameters.
- cntrlParam.ioCompletion = nil;
- cntrlParam.ioVRefNum = 0;
- cntrlParam.ioCRefNum = pAVTDriverData->driverRefNum;
- cntrlParam.csCode = cscAVTCommand;
- *((Ptr *) &(cntrlParam.csParam[0])) = (Ptr) pAVTInterfaceParams;
-
- // Call the driver.
- status = PBControlSync ((ParmBlkPtr) &cntrlParam);
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // OpenAVTDriver
- //
- // This routine opens a connection to the given driver.
- //
-
- OSStatus OpenAVTDriver (
- AVTDriverID avtDriverID)
- {
- AVTDriverDataPtr pAVTDriverData;
- OSStatus status = noErr;
-
- // Get the driver data from the ID.
- pAVTDriverData = (AVTDriverDataPtr) avtDriverID;
-
- // One more connection.
- pAVTDriverData->numConnections++;
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // CloseAVTDriver
- //
- // This routine closes a connection to the given driver.
- //
-
- OSStatus CloseAVTDriver (
- AVTDriverID avtDriverID)
- {
- AVTDriverDataPtr pAVTDriverData;
- OSStatus status = noErr;
-
- // Get the driver data from the ID.
- pAVTDriverData = (AVTDriverDataPtr) avtDriverID;
-
- // One less connection.
- pAVTDriverData->numConnections--;
-
- // Dispose driver if device has been disconnected.
- if ((pAVTDriverData->deviceDisconnected) && (pAVTDriverData->numConnections == 0))
- DisposeAVTDriver (avtDriverID);
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // GetNextAVTClientEvent
- //
- // This routine checks if any client Apple Events are pending and sends any
- // that are.
- //
-
- OSStatus GetNextAVTClientEvent(
- AVTClientID avtClientID)
- {
- AVTClientDataPtr pAVTClientData;
- ClientAppleEventDataPtr pClientAppleEventData;
- AppleEvent reply;
- OSStatus status = noErr,
- queueStatus = noErr;
-
- // Get client data from ID.
- pAVTClientData = (AVTClientDataPtr) avtClientID;
-
- // Send any client Apple Events on queue.
- while (queueStatus == noErr)
- {
- queueStatus = PBDequeueFirst (pAVTClientData->clientAppleEventQueue,
- (QElemPtr *) &pClientAppleEventData);
- if (queueStatus == noErr)
- {
- // Send the Apple Event.
- AESend (&(pClientAppleEventData->clientAppleEvent),
- &reply,
- pClientAppleEventData->sendMode,
- pClientAppleEventData->sendPriority,
- pClientAppleEventData->timeOutInTicks,
- pClientAppleEventData->idleProc,
- pClientAppleEventData->filterProc);
-
- // Dispose of reply.
- AEDisposeDesc (&reply);
-
- // We're now done with the client Apple Event data record.
- DisposeClientAppleEventData (pClientAppleEventData);
- }
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Private routines.
- //
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // InitializeAVTFamily
- //
- // This routine initializes the AV transport control driver family.
- //
-
- long InitializeAVTFamily(void)
- {
- OSStatus status = noErr;
-
- // Allocate family global record.
- gpAVTFamilyData =
- (AVTFamilyDataPtr) PoolAllocateResident (sizeof (AVTFamilyData), true);
- if (gpAVTFamilyData == nil)
- status = memFullErr;
-
- return ((long) status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // TerminateAVTFamily
- //
- // This routine terminates the AV transport control driver family.
- //
-
- long TerminateAVTFamily(void)
- {
- OSStatus status = noErr;
-
- // Deallocate family global record.
- if (gpAVTFamilyData != nil)
- PoolDeallocate ((Ptr) gpAVTFamilyData);
-
- return ((long) status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // InstallAVTExpert
- //
- // This routine installs the AV transport control driver expert loading
- // mechanism.
- //
-
- OSStatus InstallAVTExpert(void)
- {
- UInt32 eventTable[1];
- OSStatus status = noErr;
-
- // Register to receive generic device added events.
- eventTable[0] = kGDFDeviceAddedEvent;
- eventTable[1] = kGDFDeviceRemovedEvent;
- status = GDFRegisterDeviceEventHandlerProc
- (kNdrvTypeIsAVTransport,
- 2,
- &eventTable[0],
- AVTEventHandler,
- nil,
- &(gpAVTExpertData->gdfDeviceEventRegistrationID));
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // UninstallAVTExpert
- //
- // This routine uninstalls the AV transport control driver expert loading
- // mechanism.
- //
-
- OSStatus UninstallAVTExpert(void)
- {
- OSStatus status = noErr;
-
- // Dispose of AVTExpert data.
- if (gpAVTExpertData != nil)
- {
- // Unregister with Generic Device family.
- if (gpAVTExpertData->gdfDeviceEventRegistrationID !=
- kInvalidGDFDeviceEventRegistrationID)
- {
- status = GDFUnregisterDeviceEventHandler
- (gpAVTExpertData->gdfDeviceEventRegistrationID);
- }
-
- PoolDeallocate ((Ptr) gpAVTExpertData);
- gpAVTExpertData = nil;
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Internal routines.
- //
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // AVTEventHandler
- //
- // This proc handles device events for AV transport devices.
- //
-
- static OSStatus AVTEventHandler(
- GDFDeviceEventDataPtr pGDFDeviceEventData)
- {
- OSStatus status = noErr;
-
- // Dispatch off of device event.
- switch (pGDFDeviceEventData->deviceEvent)
- {
- case kGDFDeviceAddedEvent :
- status = AVTHandleDeviceAdded (pGDFDeviceEventData);
- break;
-
- case kGDFDeviceRemovedEvent :
- status = AVTHandleDeviceRemoved (pGDFDeviceEventData);
- break;
-
- default :
- break;
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // AVTHandleDeviceAdded
- //
- // This routine handles new devices being added.
- //
-
- static OSStatus AVTHandleDeviceAdded(
- GDFDeviceEventDataPtr pGDFDeviceEventData)
- {
- AVTDriverID avtDriverID;
- AVTDriverDataPtr pAVTDriverData;
-
- AVTClientDataPtr pAVTClientData;
-
- OSStatus status = noErr,
- clientStatus;
-
- // Check if avt driver is in our list. This will happen if device was
- // connected, opened, disconnected, and reconnected without being closed.
- // If it's in our list, set disconnected to false.
- avtDriverID = AVTFindDriver (&(pGDFDeviceEventData->deviceRegEntryID));
- if (avtDriverID != kInvalidAVTDriverID)
- {
- pAVTDriverData = (AVTDriverDataPtr) avtDriverID;
- pAVTDriverData->deviceDisconnected = false;
- }
- else
- {
- status = AVTAddDriver (pGDFDeviceEventData, &avtDriverID);
- if (status == noErr)
- pAVTDriverData = (AVTDriverDataPtr) avtDriverID;
- }
-
- // Send up notification to all clients.
- if (status == noErr)
- {
- pAVTClientData = gpAVTFamilyData->pAVTClientList;
- while (pAVTClientData != nil)
- {
- // Add the driver ID parameter to event.
- clientStatus = AEPutParamPtr
- (&(pAVTClientData->deviceAddedAppleEvent),
- kAEAVTDriverIDKey,
- kAEAVTDriverIDType,
- &avtDriverID,
- sizeof (AVTDriverID));
-
- // Send event.
- if (clientStatus == noErr)
- {
- clientStatus = AVTSendClientAppleEvent
- (pAVTClientData,
- &(pAVTClientData->deviceAddedAppleEvent),
- 0,
- kAENormalPriority,
- 0,
- nil,
- nil);
- }
-
- pAVTClientData = pAVTClientData->pNextAVTClientData;
- }
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // AVTHandleDeviceRemoved
- //
- // This routine handles devices being removed.
- //
-
- static OSStatus AVTHandleDeviceRemoved(
- GDFDeviceEventDataPtr pGDFDeviceEventData)
- {
- RegEntryIDPtr pRegEntryID;
- AVTDriverID avtDriverID;
- AVTDriverDataPtr pAVTDriverData;
- AVTClientDataPtr pAVTClientData;
- Boolean found;
- OSStatus status = noErr,
- clientStatus;
-
- // Get Name Registry entry.
- pRegEntryID = &(pGDFDeviceEventData->deviceRegEntryID);
-
- // Find driver that is being removed.
- avtDriverID = AVTFindDriver (pRegEntryID);
- if (avtDriverID != kInvalidAVTDriverID)
- {
- pAVTDriverData = (AVTDriverDataPtr) avtDriverID;
- found = true;
- }
- else
- {
- found = false;
- }
-
- // If driver was found, set it disconnected.
- if (found)
- pAVTDriverData->deviceDisconnected = true;
-
- // Dispose driver if no one has it open.
- // Otherwise, send up notification to all clients.
- if (found)
- {
- if (pAVTDriverData->numConnections == 0)
- {
- DisposeAVTDriver ((AVTDriverID) pAVTDriverData);
- }
- else
- {
- pAVTClientData = gpAVTFamilyData->pAVTClientList;
- while (pAVTClientData != nil)
- {
- // Add the driver ID parameter to event.
- clientStatus = AEPutParamPtr
- (&(pAVTClientData->deviceRemovedAppleEvent),
- kAEAVTDriverIDKey,
- kAEAVTDriverIDType,
- &avtDriverID,
- sizeof (AVTDriverID));
-
- // Send event.
- if (clientStatus == noErr)
- {
- clientStatus = AVTSendClientAppleEvent
- (pAVTClientData,
- &(pAVTClientData->deviceRemovedAppleEvent),
- 0,
- kAENormalPriority,
- 0,
- nil,
- nil);
- }
-
- pAVTClientData = pAVTClientData->pNextAVTClientData;
- }
- }
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // AVTSendClientAppleEvent
- //
- // This routine queues an Apple Event to be sent to a client for device
- // notification.
- //
-
- static OSErr AVTSendClientAppleEvent(
- AVTClientDataPtr pAVTClientData,
- const AppleEvent *theAppleEvent,
- AESendMode sendMode,
- AESendPriority sendPriority,
- long timeOutInTicks,
- AEIdleUPP idleProc,
- AEFilterUPP filterProc)
- {
- ClientAppleEventDataPtr pClientAppleEventData = nil;
- OSErr err = noErr;
-
- // Create data for the client Apple Event.
- pClientAppleEventData = (ClientAppleEventDataPtr)
- PoolAllocateResident (sizeof (ClientAppleEventData), true);
- if (pClientAppleEventData != nil)
- {
- pClientAppleEventData->sendMode =
- (sendMode & ~(kAENoReply | kAEQueueReply | kAEWaitReply)) | kAENoReply;
- pClientAppleEventData->sendPriority = sendPriority;
- pClientAppleEventData->timeOutInTicks = timeOutInTicks;
- pClientAppleEventData->idleProc = idleProc;
- pClientAppleEventData->filterProc = filterProc;
- }
- else
- {
- err = memFullErr;
- }
-
- // Copy the given Apple Event.
- if (err == noErr)
- {
- err = AEDuplicateDesc (theAppleEvent,
- &(pClientAppleEventData->clientAppleEvent));
- if (err == noErr)
- pClientAppleEventData->clientAppleEventValid = true;
- }
-
- // Queue the client Apple Event to be sent at a good time.
- if (err == noErr)
- {
- err = PBEnqueueLast ((QElemPtr) pClientAppleEventData,
- pAVTClientData->clientAppleEventQueue);
- }
-
- // Clean up on error.
- if (err != noErr)
- DisposeClientAppleEventData (pClientAppleEventData);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // DisposeClientAppleEventData
- //
- // This routine disposes of the client Apple Event record.
- //
-
- static void DisposeClientAppleEventData(
- ClientAppleEventDataPtr pClientAppleEventData)
- {
- if (pClientAppleEventData != nil)
- {
- // Dispose of Apple Event copy.
- if (pClientAppleEventData->clientAppleEventValid)
- AEDisposeDesc (&(pClientAppleEventData->clientAppleEvent));
-
- // Dispose of client Apple Event data record.
- PoolDeallocate ((Ptr) pClientAppleEventData);
- }
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // AVTCreateClientAppleEventQueue
- //
- // This routine creates a client AppleEvent queue.
- //
-
- static OSStatus AVTCreateClientAppleEventQueue(
- AVTClientDataPtr pAVTClientData)
- {
- OSStatus status = noErr;
-
- status = PBQueueCreate (&(pAVTClientData->clientAppleEventQueue));
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // AVTDisposeClientAppleEventQueue
- //
- // This routine disposes of a client AppleEvent queue.
- //
-
- static OSStatus AVTDisposeClientAppleEventQueue(
- AVTClientDataPtr pAVTClientData)
- {
- ClientAppleEventDataPtr pClientAppleEventData;
- OSStatus status = noErr;
-
- if (pAVTClientData->clientAppleEventQueue != nil)
- {
- // Dispose of any client AppleEvents still in queue.
- while (status == noErr)
- {
- status = PBDequeueFirst (pAVTClientData->clientAppleEventQueue,
- (QElemPtr *) &pClientAppleEventData);
- if (status == noErr)
- DisposeClientAppleEventData (pClientAppleEventData);
- }
-
- // Delete queue.
- PBQueueDelete (pAVTClientData->clientAppleEventQueue);
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // AVTAddDriver
- //
- // This routine adds a new driver.
- //
-
- static OSStatus AVTAddDriver(
- GDFDeviceEventDataPtr pGDFDeviceEventData,
- AVTDriverID *pAVTDriverID)
- {
- RegEntryIDPtr pRegEntryID;
- AVTDriverID avtDriverID = kInvalidAVTDriverID;
- AVTDriverDataPtr pAVTDriverData;
-
- OSStatus status = noErr;
-
- // Get Name Registry entry.
- pRegEntryID = &(pGDFDeviceEventData->deviceRegEntryID);
-
- // Add AV transport control driver record to list.
- if (status == noErr)
- status = CreateAVTDriverID (&avtDriverID);
-
- // Get the driver data from the ID.
- if (status == noErr)
- pAVTDriverData = (AVTDriverDataPtr) avtDriverID;
-
- // Fill in driver data record.
- if (status == noErr)
- {
- pAVTDriverData->deviceRegistryID = *pRegEntryID;
- pAVTDriverData->driverRefNum = pGDFDeviceEventData->driverRefNum;
- }
-
- // Open driver.
- if (status == noErr)
- {
- status = OpenInstalledDriver (pGDFDeviceEventData->driverRefNum, fsRdWrPerm);
- if (status == noErr)
- pAVTDriverData->driverOpened = true;
- }
-
- // Clean up on error.
- if (status != noErr)
- {
- if (avtDriverID != kInvalidAVTDriverID)
- DisposeAVTDriver (avtDriverID);
- }
-
- // Return results.
- if (status == noErr)
- *pAVTDriverID = avtDriverID;
- else
- *pAVTDriverID = kInvalidAVTDriverID;
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // CreateAVTDriverID
- //
- // This routine creates a new AV transport control driver record, adds it to
- // the family list, and returns the ID for the new driver record.
- //
-
- static OSStatus CreateAVTDriverID(
- AVTDriverID *pAVTDriverID)
- {
- AVTDriverDataPtr pAVTDriverData = nil,
- pNextAVTDriverData;
- OSStatus status = noErr;
-
- // Allocate AV transport control driver record.
- pAVTDriverData = (AVTDriverDataPtr)
- PoolAllocateResident (sizeof (AVTDriverData), true);
- if (pAVTDriverData != nil)
- pAVTDriverData->avtDriverID = (AVTDriverID) pAVTDriverData;
- else
- status = memFullErr;
-
- // Add driver record to family list.
- if (status == noErr)
- {
- pNextAVTDriverData = gpAVTFamilyData->pAVTDriverList;
- pAVTDriverData->pNextAVTDriverData = pNextAVTDriverData;
- gpAVTFamilyData->pAVTDriverList = pAVTDriverData;
- gpAVTFamilyData->numAVTDrivers++;
- }
-
- // Return result and clean up on error.
- if (status == noErr)
- {
- *pAVTDriverID = pAVTDriverData->avtDriverID;
- }
- else
- {
- if (pAVTDriverData != nil)
- PoolDeallocate ((Ptr) pAVTDriverData);
-
- *pAVTDriverID = kInvalidAVTDriverID;
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // DisposeAVTDriver
- //
- // This routine disposes of the given driver. It will call the driver to
- // terminate itself, dispose of the driver memory closure, remove the driver
- // data record from our list, and dispose of the driver data record.
- //
-
- static void DisposeAVTDriver(
- AVTDriverID avtDriverID)
- {
- AVTDriverDataPtr pAVTDriverData,
- pSearchAVTDriverData,
- pPrevAVTDriverData;
- THz currentZone;
-
- // Switch to system zone.
- currentZone = GetZone ();
- SetZone (SystemZone ());
-
- // Get the driver data from the ID.
- pAVTDriverData = (AVTDriverDataPtr) avtDriverID;
-
- // Close the driver.
- if (pAVTDriverData->driverOpened)
- CloseDriver (pAVTDriverData->driverRefNum);
-
- // Remove driver record from family list.
- pSearchAVTDriverData = gpAVTFamilyData->pAVTDriverList;
- pPrevAVTDriverData = nil;
- while ((pSearchAVTDriverData != nil) &&
- (pSearchAVTDriverData != pAVTDriverData))
- {
- pPrevAVTDriverData = pSearchAVTDriverData;
- pSearchAVTDriverData =
- pSearchAVTDriverData->pNextAVTDriverData;
- }
- if (pSearchAVTDriverData != nil)
- {
- if (pPrevAVTDriverData != nil)
- {
- pPrevAVTDriverData->pNextAVTDriverData =
- pAVTDriverData->pNextAVTDriverData;
- }
- else
- {
- gpAVTFamilyData->pAVTDriverList =
- pAVTDriverData->pNextAVTDriverData;
- }
- }
- gpAVTFamilyData->numAVTDrivers--;
-
- // Deallocate driver data record.
- PoolDeallocate ((Ptr) pAVTDriverData);
-
- // Switch zone back.
- SetZone (currentZone);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // AVTFindDriver
- //
- // This routine finds the AVT driver corresponding to the given Name Registry
- // entry.
- //
-
- static AVTDriverID AVTFindDriver(
- RegEntryIDPtr pRegEntryID)
- {
- AVTDriverID avtDriverID;
- AVTDriverDataPtr pAVTDriverData;
- Boolean found;
-
- // Find driver that has matching name registry entry.
- pAVTDriverData = gpAVTFamilyData->pAVTDriverList;
- found = false;
- while ((pAVTDriverData != nil) && (!found))
- {
- if (RegistryEntryIDCompare (&(pAVTDriverData->deviceRegistryID), pRegEntryID))
- found = true;
- else
- pAVTDriverData = pAVTDriverData->pNextAVTDriverData;
- }
-
- // Set return value.
- if (found)
- avtDriverID = (AVTDriverID) pAVTDriverData;
- else
- avtDriverID = kInvalidAVTDriverID;
-
- return (avtDriverID);
- }
-